<script setup>
import { ref, onMounted, watch } from 'vue'
import { Search } from '@element-plus/icons-vue'
import myAxios from '../../../plugins/myAxios'
import { ElMessage } from 'element-plus'

// 搜索条件
const searchForm = ref({
  itemName: '',
  startTime: '',
  endTime: ''
})

// 时间范围
const dateRange = ref([])

// 监听时间范围变化
watch(dateRange, (newVal) => {
  if (newVal) {
    searchForm.value.startTime = newVal[0]
    searchForm.value.endTime = newVal[1]
  } else {
    searchForm.value.startTime = ''
    searchForm.value.endTime = ''
  }
})

// 训练列表数据
const trainingList = ref([])

// 获取训练列表数据
const fetchTrainingList = async () => {
  try {
    const res = await myAxios.get('/student/training/page', {
      params: {
        itemName: searchForm.value.itemName,
        startTime: searchForm.value.startTime,
        endTime: searchForm.value.endTime
      }
    })
    trainingList.value = res.records;
  } catch (error) {
    ElMessage.error(error.message || '获取训练列表失败')
  }
}

// 搜索方法
const handleSearch = () => {
  fetchTrainingList()
}

// 页面加载时获取数据
onMounted(() => {
  fetchTrainingList()
})

// 处理图片URL数组
const getFirstImage = (imageUrls) => {
  if (!imageUrls) return ''
  try {
    const images = JSON.parse(imageUrls)
    return images[0] || ''
  } catch {
    return ''
  }
}
</script>

<template>
  <div class="training-container">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :model="searchForm" inline>
        <el-form-item label="训练项目">
          <el-input
            v-model="searchForm.itemName"
            placeholder="请输入训练项目名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="dateRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">
            搜索
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 视频列表区域 -->
    <div class="video-grid">
      <el-card v-for="item in trainingList"
               :key="item.trainingId"
               class="video-card">
        <div class="card-content">
          <div class="video-header">
            <h3 class="item-name text-ellipsis">{{ item.itemName }}</h3>
            <el-tag size="small" type="info" class="time-tag">{{ item.createTime }}</el-tag>
          </div>
          <div class="video-body">
            <p class="training-content">{{ item.trainingContent }}</p>
            <div class="video-thumbnail">
          <video
              v-if="item.videoUrl"
              :src="item.videoUrl"
              controls
              preload="metadata"
              class="video-preview">
          </video>
          <img
              v-else
              :src="getFirstImage(item.imageUrls)"
              :alt="item.itemName"
              class="image-preview"
          />
        </div>
          </div>
          <div class="video-footer">
            <div class="creator-info">
              <el-avatar :size="24" class="creator-avatar">
                {{ item.createBy?.charAt(0) }}
              </el-avatar>
              <span class="creator-name text-ellipsis">{{ item.createBy || '未知用户' }}</span>
            </div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<style scoped>
.training-container {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.video-card {
  height: 100%;
  display: flex;
  flex-direction: column;
  transition: transform 0.2s ease;
}

.video-card:hover {
  transform: translateY(-5px);
}

.video-thumbnail {
  position: relative;
  padding-top: 56.25%; /* 16:9 宽高比 */
  overflow: hidden;
  border-radius: 8px 8px 0 0;
  background-color: #f5f5f5;
}

.video-preview,
.image-preview {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.card-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 12px;
}

.video-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 8px;
}

.item-name {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  line-height: 1.4;
  flex: 1;
}

.time-tag {
  flex-shrink: 0;
  margin-top: 2px;
}

.text-ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.video-body {
  flex: 1;
  margin-bottom: 12px;
}

.training-content {
  margin: 0;
  font-size: 14px;
  line-height: 1.6;
  color: #666;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.video-footer {
  border-top: 1px solid #f0f0f0;
  padding-top: 12px;
  margin-top: auto;
}

.creator-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.creator-avatar {
  flex-shrink: 0;
  background: #409eff;
}

.creator-name {
  font-size: 14px;
  color: #666;
  max-width: calc(100% - 32px);
}
</style>
